<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>新增数据源</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}" media="screen"/>
    <link rel="stylesheet" th:href="@{/static/css/layui.css}" media="all"/>
</head>
<body>
<div class="layui-card-header" style="font-size: 15px;color: #999;font-family: 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif"><div>[[${data.name}]]  |  [[${data.capacity}]]</div></div>
<hr class="layui-border-green">
<div class="layui-field-box">
    <form class="layui-form" action="" id="addFrom">
        <div class="layui-form-item">
            <label class="layui-form-label">连接名称</label>
            <div class="layui-input-block">
                <input type="text" name="dbName" id="dbName" lay-verify="required" autocomplete="off" lay-affix="clear"
                       placeholder="数据源名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">驱动名称</label>
            <div class="layui-input-block">
                <input type="text" name="driverClass" id="driverClass" lay-verify="required" lay-affix="clear" th:value="${data.driverClass}"
                       placeholder="驱动名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">连接地址</label>
            <div class="layui-input-block">
                <input type="text" name="dbUrl" id="dbUrl" lay-verify="required" placeholder="连接地址" th:value="${data.url}"
                       lay-affix="clear"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">数据库账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="dbAccount" id="dbAccount" placeholder="数据库登录账号"
                           lay-affix="clear"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">数据库密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="dbPassword" id="dbPassword" placeholder="数据库登录密码"
                           lay-affix="eye"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">初始连接</label>
                <div class="layui-input-inline">
                    <input type="text" name="initialSize" id="initialSize" value="5" placeholder="初始连接"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">最大连接</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxActive" id="maxActive" value="20" placeholder="最大连接"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">空闲连接</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxIdle" id="maxIdle" value="5" placeholder="空闲连接"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">等待时间(ms)</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxWait" id="maxWait" value="5000" placeholder="等待时间"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">验证语句</label>
                <div class="layui-input-inline">
                    <input type="text" name="dbCheckUrl" id="dbCheckUrl" placeholder="验证语句" th:value="${data.dbCheckUrl}"
                           lay-affix="clear"
                           autocomplete="off" class="layui-input">
                    <input type="hidden" name="dbState" id="dbState" value="有效" class="layui-input">
                </div>
            </div>
        </div>
        <div class="page-footer">
            <div class="btn-list">
                <div class="btnlist">
                    <button class="layui-btn layui-btn-primary layui-border-red" onclick="goPage('1');return false;">
                        <i class="layui-icon layui-icon-close-fill">&nbsp;</i>返回上一步
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-orange" lay-submit lay-filter="testUrl">
                        <i class="layui-icon layui-icon-link">&nbsp;</i>测试连接
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-green" lay-submit lay-filter="save" lay-type="saveContinue">
                        <i class="layui-icon  layui-icon-add-circle">&nbsp;</i>保存并继续
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/static/layui.js}"></script>
<script th:src="@{/static/lay-config.js}"></script>
<script th:replace="~{base::ctx}"></script>
<script>

    layui.use(['layer', 'form'], function () {
        const layer = layui.layer;
        const form = layui.form;
        /**
         * 保存
         */
        form.on('submit(save)', function (data) {
            const field = data.field;
            const elem = data.elem;
            const layType = elem.getAttribute("lay-type");
            post(ctx + "dataSourceManager/addDataSource", field, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                    if (res.code === 200) {
                        if (layType === "saveClose") {
                            refresh();
                        } else {
                            location.reload();
                        }
                    }
                });
            })
            return false;
        });

        /**
         * 测试链接
         */
        form.on('submit(testUrl)', function (data) {
            const field = data.field;
            post(ctx + "dataSourceManager/checkUrl", field, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2});
            })
            return false;
        });
    });
    function goPage() {
        window.location.href = ctx + 'dataSourceManager/dataSourceChoice';
    }
</script>
</body>
</html>
